package sng.modules.view.components.city.strategy
{
	import flash.events.TimerEvent;
	
	import mx.controls.Alert;
	import mx.states.AddItems;
	import mx.validators.EmailValidator;
	
	import sng.data.info.GlobalInfo;
	import sng.data.info.GlobalTimer;
	import sng.modules.model.vo.BuildingVO;
	import sng.modules.model.vo.MainVO;
	import sng.modules.model.vo.UserBuildingVO;
	import sng.modules.view.components.city.logic.unit.BaseBuilding;
	import sng.modules.view.components.city.state.BuildGatherState;
	import sng.modules.view.components.city.state.BuildIdleState;
	import sng.modules.view.components.city.state.BuildNormalState;
	import sng.modules.view.components.city.state.EnumBuildState;
	import sng.utils.TimeUtil;
	
	/**
	 * 
	 * @author leonliu
	 * 住宅策略类
	 * 
	 */	
	public class HouseStrategy extends Strategy
	{
		public function HouseStrategy(building:BaseBuilding, bvo:BuildingVO, userbvo:UserBuildingVO)
		{
			super(building, bvo, userbvo);
			if (m_userbvo.population==0)
				m_userbvo.population = m_bvo.populationDownLimit;//人口
		}
		
		private var gatherNum:uint=0;//收获金钱次数，第5次会使人口增加10
		
		public override function createBuildingStrategy():BuildingIncrement
		{
			//1.计算新建开始的数值变化
			GlobalInfo.mainvo.money=GlobalInfo.mainvo.money-m_bvo.buildingprice;
			
			m_building.addPopulationAnimation(10);
			
			return new BuildingIncrement();
		}
		
		public override function addBuildingStrategy():void
		{
			
			//1.计算新建后的数值变化
			GlobalInfo.mainvo.experience+=m_bvo.dropExp;//增加经验值 
			GlobalInfo.mainvo.personnum+=m_bvo.populationDownLimit;
			if (m_userbvo.isIdle)
			{
				m_userbvo.nextState=EnumBuildState.NORMAL_STATE;
				
				buildStateManage(EnumBuildState.IDLE_STATE);
				m_building.itempstate=EnumBuildState.NORMAL_STATE;
				GlobalTimer.timeFunctionList.addItem({id:m_userbvo.buildingid,time:m_bvo.countdown, func:gatherTimerHandler,run:false});
				m_building.uVO.countdownTime = Number(m_building.vo.countdown)+TimeUtil.getTimeSecond();
			}
			else
			{
				//2.新建后建筑变成 normal 
				buildStateManage(EnumBuildState.NORMAL_STATE);
				//3.增加  收租倒计时
				GlobalTimer.timeFunctionList.addItem({id:m_userbvo.buildingid,time:m_bvo.countdown, func:gatherTimerHandler,run:true});
				m_building.uVO.countdownTime = Number(m_building.vo.countdown)+TimeUtil.getTimeSecond();
				
			}
		}
		
		/**
		 *收获金钱后触化此方法 
		 * 
		 */		
		public override function gatherBuildingStrategy():Boolean
		{
			
			if (super.gatherBuildingStrategy())
			{
				//1.计算新建后的数值变化
				gatherNum+=1;
				if (gatherNum==m_building.vo.populationUpTimes)
					GlobalInfo.mainvo.personnum+=(m_bvo.populationUpLimit-m_bvo.populationDownLimit);
				
				
//				//2.修改状态为正常状态
//				if (m_userbvo.isIdle)
//				{
//					m_userbvo.nextState=EnumBuildState.NORMAL_STATE;
//					buildStateManage(EnumBuildState.IDLE_STATE);
//				}
				//else
				//{
					buildStateManage(EnumBuildState.NORMAL_STATE);
					GlobalTimer.timeFunctionList.addItem({id:m_userbvo.buildingid,time:m_bvo.countdown, func:gatherTimerHandler,run:true});
					m_building.uVO.countdownTime = Number(m_building.vo.countdown)+TimeUtil.getTimeSecond();
				//}
				return true;
			}
			else
				return false;
		}
		
		/**
		 *收租倒计时完成时触化 
		 * 
		 */		
		private function gatherTimerHandler():void
		{
			buildStateManage(EnumBuildState.GATHER_STATE);
		}
		
		/**
		 *心跳 
		 * 
		 */		
		protected override function heartBeat(event:TimerEvent):void
		{
			tip.gotoAndStop(tipFrame);
			switch(m_building.currentState.getiStateType())
			{
				case EnumBuildState.NORMAL_STATE:
				{
					var time:Number = TimeUtil.getTimeSecond();
					tip.gotoAndStop("house");
					tip.timeText.text = TimeUtil.getTimeStringFromSecond(GlobalTimer.getFuncTime(m_userbvo.buildingid));//TimeUtil.getTimeStringFromSecond(m_userbvo.countdownTime-time);
					tip.populationText.text = m_userbvo.population+"/"+m_bvo.populationUpLimit;
					if (GlobalTimer.getFuncTime(m_userbvo.buildingid)<=0)//if(m_userbvo.countdownTime<time)
					{		
						//时间到
						tip.timeText.text ="";
						GlobalTimer.removeHeartBeatEventListener(heartBeat);//移除监听事件
					}					
					break;
				}
				case EnumBuildState.IDLE_STATE:
				{
					m_building.uVO.countdownTime +=1;//计时时间往后续延
					break;
				}
				case EnumBuildState.MOVE_STATE:
				{
					m_building.uVO.countdownTime +=1;//计时时间往后续延
					break;
				}
				default:
				{
					
				}
			}			
		}
		
		
		/**
		 * 
		 * @param iState
		 * 民房建筑各状态下的UI管理
		 * 
		 */		
		public override function buildStateManage(iState:uint):void
		{
			m_building.removeTip();
			if (iState!=EnumBuildState.IDLE_STATE)
			{
				//if (m_building.currentState&& m_building.currentState.getiStateType()==EnumBuildState.IDLE_STATE)
				
				GlobalTimer.setFuncRun(m_userbvo.buildingid,true);
			}
			
			
			m_building.setBuildState(EnumBuildState.getStateById(iState));
			switch (iState)
			{
				case EnumBuildState.NEW_STATE:
					tipFrame = "building_create";
					break;
				case EnumBuildState.NORMAL_STATE:
					tipFrame = "house";
					break;
				case EnumBuildState.GATHER_STATE://收租
					tipFrame = "building_gathing";
					m_building.addCoinTip();//调用UI弹出收租提示
					break;	
				case EnumBuildState.IDLE_STATE://闲置状态
					tipFrame = "idle";
					//ui变化
					//2.设置
					m_building.setIdleUI();
					GlobalTimer.setFuncRun(m_userbvo.buildingid,false);
					break;
			}
		}
		
	}
}